package com.initialize.appapi.controller;

import com.initialize.appapi.service.UserService;
import com.initialize.pub.pojo.Result;
import com.initialize.pub.service.SignService;
import com.initialize.pub.token.TokenService;
import com.initialize.pub.token.UserLoginToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @program: Initialization_Project
 * @description: 用户Controller
 * @author: Mr.Jane
 * @create: 2019-10-10 22:25
 **/
@Api(tags = "用户API")
@RestController
@Slf4j
@CrossOrigin
@RequestMapping(value = "/user")
public class UserController {


    private final
    UserService userService;

    private final
    SignService signService;

    @Autowired
    TokenService tokenService;

    public UserController(UserService userService, SignService signService) {
        this.userService = userService;
        this.signService = signService;
    }

    @ApiOperation(value = "客户端--用户登录", notes = "用户登录")
    @PostMapping("/login/{sign}")
    public Result login(@PathVariable String sign, @RequestBody Map<String,Object> stringObjectMap) throws Exception {
        log.info(stringObjectMap.toString());
        int i = signService.judgingByMap(stringObjectMap, sign);
        if (i == 1) {
            return Result.error("请勿重复请求");
        }
        return userService.login(stringObjectMap);
    }
    @UserLoginToken
    @ApiOperation(value = "客户端--获取用户信息", notes = "获取用户信息")
    @GetMapping("/user-info")
    public Result UserInfo(HttpServletRequest httpServletRequest) throws Exception {
        int id = tokenService.getId(httpServletRequest);
        return userService.UserInfo(id);
    }

}
